package io.peng.j2se.lab;

/**
 * Created by eric on 16/3/15.
 */
public class Consumer implements Runnable {
    private int random() {
        return (int) Math.floor(Math.random() * 10);
    }

    public void run() {
        System.out.println("Consumer is started");
        while (true) {
            if (MySingleton.getInstance().getBalance() > 10) {
                int value = random();
                MySingleton.getInstance().setBalance(MySingleton.getInstance().getBalance() - value);
                System.out.println("Consumer substract "+value+", balance: "+MySingleton.getInstance().getBalance());
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
